// pages/home/home.js
import {
  showText
} from "../../utils/util"
import {
  apiUrl
} from '@/api/request'
import {
  takephonenumber,takeInviteAward,registe_member_info
} from '@/api/register'
import {
  getgemomemberuser
} from '@/api/my'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUri: getApp().globalData.imgUri,
    showWelcome: false, // 是否显示欢迎页
    acsel1: false,
    acsel2: false,
    phoneState: true,
    phone: '',
    // 隐私政策
    showPropPrivacy:false,
    // 用户协议
    shopPropAgreement:false,
    inviterId:0,
    showPopup: false,
    avatarUrl: getApp().globalData.imgUri + '/my/avatarUrl.png',
    nickName: 'GEMO会员',
    is_set_nickname:0,
  },
  selone() {
    this.setData({
      acsel1: !this.data.acsel1
    })
  },
  selone2() {
    this.setData({
      acsel2: !this.data.acsel2
    })
  },
  phoneInput(e) {
    this.setData({
      phone: e.detail.value,
      phoneState: true
    })
  },
  cli_privacy(){
    this.setData({
      showPropPrivacy:true
    })
  },
  closePropPrivacy(){
    this.setData({
      showPropPrivacy:false
    })
  },
  cli_agreement(){
    this.setData({
      shopPropAgreement:true
    })
  },
  closePropAgreement(){
    this.setData({
      shopPropAgreement:false
    })
  },
  closeDialog(e){
    var key = e.currentTarget.dataset.key;
    this.setData({
      [key]:false
    })
  },
  // 领取积分
  confirmWelcome() {
    this.setData({
      showWelcome: false
    })
  },
  register() {
    let t = this;
    if (t.data.phone) {
        if (t.data.acsel1) {
          if (t.data.acsel2) {
            t.f_getmemberinfo();

          } else {
            showText('请阅读并同意商业信息')
          }
        } else {
          showText('请阅读并同意隐私政策')
        }

    } else {
      t.setData({
        phoneState: false
      })
    }
  },
  getPhoneNumber(evt) {
    let t = this;
    var errMsg = evt.detail.errMsg;
    if(errMsg == "getPhoneNumber:ok"){
      var code = evt.detail.code;
      takephonenumber({phoneCode:code}).then(res=>{
        if(res.code == 200){
          t.setData({
            phone: res.data,
            phoneState: true
          });

        }
      });
    }else{

    }
  },
  f_getmemberinfo(){
    let t = this;
      registe_member_info({phonenumber:t.data.phone}).then(res=>{
      if(res.code == 200){
        wx.setStorageSync('phone', t.data.phone);
        if(t.data.inviterId > 0){
          t.takeInviteAward();
        }else{
          let new_url = "";
          let url = wx.getStorageSync('module_url');
          if(!url){
            new_url = "/pages/myGemo/myGemo";
          }else{
            new_url = url;
          }
          wx.removeStorageSync('module_url');
          wx.redirectTo({
            url:new_url,
          });
        }
      }
    });
  },

  takeInviteAward(){
    let t = this;
    takeInviteAward({}).then(res=>{
      wx.removeStorageSync('inviterld');
      if(res.code == 200){
        wx.showModal({
          title: '提示',
          content: '恭喜你获得好友积分礼赠',
          showCancel: false,
          success (res) {
            if (res.confirm) {
              let new_url = "";
              let url = wx.getStorageSync('module_url');
              if(!url){
                new_url = "/pages/myGemo/myGemo";
              }else{
                new_url = url;
              }
              wx.removeStorageSync('module_url');
              wx.redirectTo({
                url:new_url,
              });
            }
          }
        })
      }
    });
  },
  back() {
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let t = this;
    let inviterId = wx.getStorageSync('inviterId');
    if(inviterId){
      t.setData({
        inviterId: inviterId,
        showWelcome:true,
      });
    }
    getgemomemberuser({inviterId:inviterId}).then(res => {
      if (res.code == 200) {
        wx.setStorageSync('getgemomemberuser', res.data);
        if(res.data.phonenumber){
          wx.setStorageSync('phone', res.data.phonenumber);
          wx.reLaunch({
            url:'/pages/home/home'
          });
        }
      }
    });
  },
  cli_auth(){
    let t = this;

    this.setData({
      showPopup: true
    });
  },
  closeAuth(e) {
    console.log(e)
    this.setData({
      showPopup: false
    })
    if (e.detail) {
      this.getgemomemberuserFun()
    }
  },
  getgemomemberuserFun() {
    getgemomemberuser({}).then(res => {
      console.log("aaaaaaaaa")
      var avatarUrl = res.data.avatarUrl;
      var nickName = res.data.nickName;
      if (avatarUrl) {
        this.setData({
          avatarUrl: getApp().globalData.cdn + avatarUrl
        })
      }
      if (nickName) {
        this.setData({
          nickName: nickName
        })
      }
      this.setData({
        is_set_nickname: 1
      })
    });
  },
  // 设置头像
  setAvatarUrl(e) {
    console.log(e)
    this.setData({
      avatarUrl: e.detail
    })
    let url = apiUrl + '/file/uploadFile'
    // 上传图片
    wx.uploadFile({
      filePath: e.detail,
      name: 'file',
      header: {
        'content-type': 'application/json',
        'Authorization': wx.getStorageSync('access_token')
      },
      url: url,
      success: (res) => {
        let res1 = JSON.parse(res.data)
        console.log(res1.data.url)
        this.setData({
          avatarUrl: getApp().globalData.cdn + res1.data.url
        })
      },
      fail: (err) => {
        console.log('Url:', url, ' err:', err)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: getApp().globalData.share_title,
      path: getApp().globalData.share_url,
      imageUrl: getApp().globalData.share_image,
    }
  }
})